<?php
namespace app\index\controller;

use app\index\model\Wechat as WechatSdk;
use app\common\controller\Base;

class Wechat extends Base
{
    private $wechat;

    public function index()
    {
        $wechatConfig = config('wechat');

        $options = array(
            'token' => $wechatConfig['wechat_token'],             //填写你设定的key
            'encodingaeskey' => $wechatConfig['wechat_encodingaeskey'],   //填写加密用的EncodingAESKey，如接口为明文模式可忽略
            'appid' => $wechatConfig['wechat_appid'],            //填写高级调用功能的app id
            'appsecret' => $wechatConfig['wechat_appsecret']            //填写高级调用功能的密钥
        );
        $this->wechat = new WechatSdk($options);
        // $wechat->valid();
        $type = $this->wechat->getRev()->getRevType();
        switch ($type) {
//            case WechatSDK::MSGTYPE_TEXT:
//                $this->applySuccess();
//                break;
            case WechatSDK::EVENT_SUBSCRIBE:
                $contentStr = "感谢您关注美育儿！\n";
                $this->wechat->text($contentStr)->reply();
                break;
            default:
                $contentStr = "感谢您关注美育儿！\n";
                $this->wechat->text($contentStr)->reply();
        }
        exit();
    }

    // 报名成功
    public function applySuccess()
    {
        $wechatConfig = config('wechat');

        $options = array(
            'token' => $wechatConfig['wechat_token'],             //填写你设定的key
            'encodingaeskey' => $wechatConfig['wechat_encodingaeskey'],   //填写加密用的EncodingAESKey，如接口为明文模式可忽略
            'appid' => $wechatConfig['wechat_appid'],            //填写高级调用功能的app id
            'appsecret' => $wechatConfig['wechat_appsecret']            //填写高级调用功能的密钥
        );
        $this->wechat = new WechatSdk($options);
        $description = "您已经报名了\r\n『2018美育儿萌动中国季度宝宝大奖晒』\r\n结束时间：2018年4月30日 23:59:59\r\n\r\n每天关注一下宝宝被投票情况进展，如果票数不够，可通过“个人中心—>充值—>答谢设置”操作，再每天抽取那么丢丢时间，为宝宝拉一票，效果会更加明显，奖品非你莫属哟^_^\r\n另外，长按下面一条消息中的海报图片，保存到手机相册，发送给亲朋好友，可为宝宝拉票哟！\r\n点击进入即可关注宝宝投票情况";
        $newsData = [
            [
                'title' => '报名成功！投票已开始，点击可直接关注亲朋好友的投票或拉票支持',
                'description' => $description,
                'picurl' => 'http://v.meiyuer.cn/static/index/img/banner_vote.png',
                'url' => 'http://v.meiyuer.cn'
            ]
        ];
        $data = [
            'touser' => 'oXQE_xFShZq2Mg6lzjS-FePRMp4Y',
            'msgtype' => 'news',
            'news' => ['articles' => $newsData]
        ];
        $this->wechat->sendCustomMessage($data);
    }
}
